package com.wechat.app.ui.screens

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.wechat.app.ui.navigation.NavRoutes
import com.wechat.app.ui.screens.chat.ChatDetailScreen
import com.wechat.app.ui.screens.chat.HomeScreen
import com.wechat.app.ui.screens.contacts.ContactsScreen
import com.wechat.app.ui.screens.contacts.ProfileScreen
import com.wechat.app.ui.screens.discover.DiscoverScreen
import com.wechat.app.ui.screens.discover.MomentsScreen
import com.wechat.app.ui.screens.me.MeScreen
import java.net.URLDecoder

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun RootScreen() {
    val navController = rememberNavController()
    com.wechat.app.ui.navigation.navController = navController
    
    var selectedTab by remember { mutableStateOf(0) }
    
    val tabs = listOf(
        "微信" to Icons.Default.Chat,
        "通讯录" to Icons.Default.Person,
        "发现" to Icons.Default.Explore,
        "我" to Icons.Default.Person
    )
    
    // Update selected tab based on current route
    val currentRoute = navController.currentDestination?.route
    LaunchedEffect(currentRoute) {
        selectedTab = when (currentRoute) {
            NavRoutes.HOME -> 0
            NavRoutes.CONTACTS -> 1
            NavRoutes.DISCOVER -> 2
            NavRoutes.ME -> 3
            else -> selectedTab
        }
    }
    
    Scaffold(
        bottomBar = {
            NavigationBar {
                tabs.forEachIndexed { index, (title, icon) ->
                    NavigationBarItem(
                        icon = { Icon(icon, contentDescription = title) },
                        label = { Text(title) },
                        selected = selectedTab == index,
                        onClick = {
                            selectedTab = index
                            val route = when (index) {
                                0 -> NavRoutes.HOME
                                1 -> NavRoutes.CONTACTS
                                2 -> NavRoutes.DISCOVER
                                3 -> NavRoutes.ME
                                else -> NavRoutes.HOME
                            }
                            navController.navigate(route) {
                                popUpTo(NavRoutes.HOME) { saveState = true }
                                launchSingleTop = true
                                restoreState = true
                            }
                        }
                    )
                }
            }
        }
    ) { paddingValues ->
        NavHost(
            navController = navController,
            startDestination = NavRoutes.HOME,
            modifier = Modifier.padding(paddingValues)
        ) {
            composable(NavRoutes.HOME) {
                HomeScreen()
            }
            composable(NavRoutes.CONTACTS) {
                ContactsScreen()
            }
            composable(NavRoutes.DISCOVER) {
                DiscoverScreen()
            }
            composable(NavRoutes.ME) {
                MeScreen()
            }
            composable(NavRoutes.MOMENTS) {
                MomentsScreen()
            }
            composable(
                route = NavRoutes.CHAT_DETAIL,
                arguments = listOf(
                    navArgument("chatName") { 
                        type = NavType.StringType
                    },
                    navArgument("chatAvatar") { 
                        type = NavType.StringType
                        nullable = true
                        defaultValue = null
                    }
                )
            ) { backStackEntry ->
                val chatName = try {
                    java.net.URLDecoder.decode(
                        backStackEntry.arguments?.getString("chatName") ?: "",
                        "UTF-8"
                    )
                } catch (e: Exception) {
                    backStackEntry.arguments?.getString("chatName") ?: ""
                }
                val chatAvatar = try {
                    val avatar = backStackEntry.arguments?.getString("chatAvatar")
                    if (avatar != null && avatar.isNotEmpty()) {
                        java.net.URLDecoder.decode(avatar, "UTF-8")
                    } else {
                        null
                    }
                } catch (e: Exception) {
                    backStackEntry.arguments?.getString("chatAvatar")
                }
                ChatDetailScreen(chatName = chatName, chatAvatar = chatAvatar)
            }
            composable(
                route = NavRoutes.PROFILE,
                arguments = listOf(
                    navArgument("contactName") { 
                        type = NavType.StringType
                    },
                    navArgument("contactAvatar") { 
                        type = NavType.StringType
                        nullable = true
                        defaultValue = null
                    }
                )
            ) { backStackEntry ->
                val contactName = try {
                    java.net.URLDecoder.decode(
                        backStackEntry.arguments?.getString("contactName") ?: "",
                        "UTF-8"
                    )
                } catch (e: Exception) {
                    backStackEntry.arguments?.getString("contactName") ?: ""
                }
                val contactAvatar = try {
                    val avatar = backStackEntry.arguments?.getString("contactAvatar")
                    if (avatar != null && avatar.isNotEmpty()) {
                        java.net.URLDecoder.decode(avatar, "UTF-8")
                    } else {
                        null
                    }
                } catch (e: Exception) {
                    backStackEntry.arguments?.getString("contactAvatar")
                }
                ProfileScreen(contactName = contactName, contactAvatar = contactAvatar)
            }
        }
    }
}

